﻿using ColorGame.Properties;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ColorGame
{
    public partial class MainForm : Form
    {
        #region 属性变量
        private static string filedatarootpath = Application.StartupPath + "\\data\\";

        private static string backimages = filedatarootpath+"\\backimages\\";


        private Bitmap sourcebitmap = null;
        private Bitmap bitmap = null;
        private Color curColor = Color.Red;
        private List<FillColor> fillColors = new List<FillColor>();
        private List<FileInfo> imagefileInfos = new List<FileInfo>();
        private int imageindex = 0;

        #endregion

        #region 初始化

        public MainForm()
        {
            InitializeComponent();
            this.DoubleBuffered = true;

            this.Text = "我学爱画画 by 明月心 QQ:277784829  v1.0.0";

        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread( ()=> { PlayMusic(); });
            thread.IsBackground = true;
            thread.Start();
            byte[] backmain = File.ReadAllBytes(backimages + "main.jpg");
            this.panelmain.BackgroundImage = new Bitmap(new MemoryStream(backmain));

            byte[] backright = File.ReadAllBytes(backimages + "right.jpg");
            this.panel功能.BackgroundImage = new Bitmap(new MemoryStream(backright));
            LoadImages();
        }
        private void PlayMusic()
        {
            //System.Media.SoundPlayer soundPlayer = new System.Media.SoundPlayer();
            //soundPlayer.SoundLocation= filedatarootpath + "\\backmusic.mp3";
            //soundPlayer.Play();
        }
        private void LoadImages(string type= "images")
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(filedatarootpath .TrimEnd('\\')+ "\\"+ type);
            if (!directoryInfo.Exists) directoryInfo.Create();
            imagefileInfos = directoryInfo.GetFiles().ToList();
            if (imagefileInfos.Count == 0) return;
            imageindex = 0;
            LoadSelectImages();
        }
        private void LoadSelectImages()
        {
         if(sourcebitmap!=null) sourcebitmap.Dispose();
            if (bitmap != null) bitmap.Dispose();
         
            var  imagebyte= File.ReadAllBytes(imagefileInfos[imageindex].FullName);
            sourcebitmap= new Bitmap(new MemoryStream(imagebyte));
            bitmap = new Bitmap(new MemoryStream(imagebyte));
            panelImagePreview.BackgroundImage = sourcebitmap;
            pictureBox.BackgroundImage = bitmap;
            pictureBox.Width = bitmap.Width;
            pictureBox.Height = bitmap.Height;
           int  x = (int)((panelmain.Width - pictureBox.Width) * 0.5);
           int y = (int)((panelmain.Height - pictureBox.Height) * 0.5);
            pictureBox.Location = new Point(x, y);
        }
        #endregion

        private void colorSelector_ColorChanged(Color color, EventArgs e)
        {
            panelCurColor.BackColor = color;
            curColor = color;
        }
       
       
       
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (bitmap == null) return;
            fillColors.Add(new FillColor() { point = e.Location, fillcolor = curColor });
            FloodFills();
        }

        private void FloodFills()
       {
            if (fillColors.Count > 0)
            {
                foreach (var item in fillColors)
                {
                   ImageHelper. FloodFill(bitmap, item.point, item.fillcolor);
                }
            }
            pictureBox.BackgroundImage = bitmap;
            pictureBox.Refresh();
        }

        /// <summary>
        /// 新的开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonNew_Click(object sender, EventArgs e)
        {
            LoadImages();
        }
        /// <summary>
        /// 加载作品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonold_Click(object sender, EventArgs e)
        {
            LoadImages("saveimages");

        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(filedatarootpath.TrimEnd('\\')+ "\\saveimages");
            if (!directoryInfo.Exists) directoryInfo.Create();

            string savefile = directoryInfo.FullName + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss")+".png";
         
            MemoryStream stream = new MemoryStream();

            bitmap.Save(stream, ImageFormat.Png);

            File.WriteAllBytes(savefile, stream.ToArray());

            
          
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 前一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            imageindex--;
            if (imageindex < 0) imageindex = 0;


            fillColors = new List<FillColor>();

            LoadSelectImages();

        }
        /// <summary>
        /// 后一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            imageindex++;
            if (imageindex >=imagefileInfos.Count) imageindex = imagefileInfos.Count-1;
            fillColors = new List<FillColor>();
            LoadSelectImages();
        }
        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {

            if (fillColors.Count == 0) return;
            fillColors.RemoveAt(fillColors.Count-1);
            bitmap =(Bitmap) sourcebitmap.Clone();
            FloodFills();
        }
     
    }
}
